package com.qf.commerce.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.AccountDto;
import com.qf.commerce.common.dto.AccountDto2;
import com.qf.commerce.common.vo.AccountVo;
import com.qf.commerce.entity.Account;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.AccountService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 增加账户
     * @param accountDto
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody AccountDto accountDto){
        try {
            return ResponseResult.success(accountService.add(accountDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }


    //删除
    @DeleteMapping("/delete")
    public ResponseResult<Integer> delete(@RequestParam List<Long> ids){
        try {
            return ResponseResult.success(accountService.delete(ids));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }


    //改
    @PutMapping("/updateA")
    public ResponseResult<Integer> updateAccount(@RequestBody Account account){
        try {
            return ResponseResult.success(accountService.updateAccount(account.getAccountId(),account));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
    @PutMapping("/updateR")
    public ResponseResult<Integer> updateRole(@RequestParam Long organizationId, @RequestParam Long roleId, @RequestParam Long accountId){
        try {
            return ResponseResult.success(accountService.updateRole(organizationId,roleId,accountId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
    @PutMapping("/updateP")
    public ResponseResult<Integer> updatePassword(@RequestParam String accountPassword, @RequestParam Long accountId){
        try {
            return ResponseResult.success(accountService.updatePassword(accountPassword,accountId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }




    /**
     * 查询
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<AccountVo>> list(String organizationName, Integer status, String accountName, String userName, String roleName,Integer page, Integer size) {
        try {
            return ResponseResult.success(accountService.selectAccount(organizationName,status,accountName,userName,roleName,page,size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }



    /**
     * 通过ID名查询账号信息
     */
    @GetMapping("/listDetail")
    public ResponseResult<List<AccountDto>> listDetail(@RequestParam Long accountId) {
        try {
            return ResponseResult.success(accountService.selectAccountDetail(accountId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }



    //个人信息
    @GetMapping("/listPersonDetail")
    public ResponseResult<List<AccountDto2>> listPersonDetail(@RequestParam Long accountId) {
        try {
            return ResponseResult.success(accountService.selectPersonDetailById(accountId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }
    @PutMapping("/updatePerson")
    public ResponseResult<Integer> updatePerson(@RequestBody Account account){
        try {
            return ResponseResult.success(accountService.updatePersonDetailById(account.getAccountId(),account));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }



}
